package cn.com.uthink.interceptor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SellerLoginFilter implements Filter {

	private String excludedPages;
	private String[] excludePageArray;

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest re = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String url = re.getRequestURI();

		boolean isExclude = false;
		for (String excludePage : excludePageArray) {
			if (url.endsWith(excludePage)) {
				isExclude = true;
				break;
			}
		}

		if (isExclude) {// 不要验证的页面
			chain.doFilter(request, response);
		} else {// 需要验证的页面
			Object webseller = re.getSession().getAttribute("webseller");
			if (webseller == null) {// 没有登陆
				String context = re.getContextPath();
				res.sendRedirect(context + "/seller/login_pre");
			} else {// 登陆成功
				chain.doFilter(request, response);
			}
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// <!-- 排除以什么结尾的页面;多个页面用逗号（,）隔开 -->
		excludedPages = config.getInitParameter("excludedPages");// 得到web.xml配置的排除页面
		excludePageArray = excludedPages.split(",");
	}

}
